package com.topcent.tioxyc.controller;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.topcent.tioxyc.constants.KeyConstant;
import com.topcent.tioxyc.pojo.vo.CostOrderFeeVO;
import com.topcent.tioxyc.pojo.vo.SaleTextResult;
import com.topcent.tioxyc.pojo.vo.SaleTextVO;
import com.topcent.tioxyc.util.ApiClient;
import com.topcent.tioxyc.util.ConnmeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author: 陈百万
 * @date: 2025/8/21 17:52
 * @Description: 销售备注转移数据
 */
@Slf4j
@RestController
public class CcChangeSalesOrderDataController {

    @GetMapping("changeSalesOrderData")
    public String changeSalesOrderData() {
        String appId = "6690c7633df0f4b90b4ef551";
        String entryId = "6690c891b1ffef3310d1b90d";

        List<SaleTextVO> saleTextVOList = new ArrayList<>();
        Header[] headers = ConnmeUtils.createHeaders();
        Map<String, Object> parameter = new HashMap<>();
        parameter.put("app_id", appId);
        parameter.put("entry_id", entryId);
        boolean fale = true;
        String zh = "";
        do {
            if (!zh.isEmpty()) {
                parameter.put("data_id", zh);
            }
            parameter.put("limit", 100);
            String jsonSale = JSONUtil.toJsonStr(parameter);
            JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", headers, jsonSale);
            String data = jdySale.getStr("data");
            JSONArray objects = JSONUtil.parseArray(data);
            List<SaleTextVO> list = JSONUtil.toList(objects, SaleTextVO.class);
            if (list.size() == 100) {
                zh = list.getLast().get_id();
            } else {
                fale = false;
            }
            saleTextVOList.addAll(list);
        } while (fale);

        // 核心：子表单备注有值则赋值
        List<SaleTextResult> resultList = new ArrayList<>();
        for (SaleTextVO vo : saleTextVOList) {
            // 主表ID和子表单判空
            if (vo.getPurchase_orders() == null || vo.getPurchase_orders().isEmpty()) {
                continue;
            }
            // 取第一个非空备注
            for (SaleTextVO.purchaseNo po : vo.getPurchase_orders()) {
                String remark = po.get_widget_1737440340378();
                if (remark != null && !remark.trim().isEmpty()) {
                    SaleTextResult result = new SaleTextResult();
                    result.set_id(vo.get_id());
                    result.set_widget_1754385003661(remark.trim());
                    resultList.add(result);

                    Map<String, Object> parameterInster = new HashMap<>();
                    parameterInster.put("app_id", appId);
                    parameterInster.put("entry_id", entryId);
                    parameterInster.put("is_start_trigger", "true");
                    parameterInster.put("data_id", result.get_id());
                    result.set_id(null);
                    parameterInster.put("data", ConnmeUtils.convertEntityToMapNull(result));
                    String jsonSalee = JSONUtil.toJsonStr(parameterInster);
                    ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, jsonSalee);

                    break; // 只取第一个，跳出循环
                }
            }
        }

        return "有效记录数：" + resultList.size();
    }


}
